/**   
 * @Title: CommonUtil.java 
 * @package com.rcxf.util.file 
 * @Description: 
 * @author zhuw
 * @date 2012-10-26 下午4:36:17 
 */
package com.contact;

import java.util.List;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.widget.Toast;

import com.android.ocean.base.ExitApplication;
import com.matang.R;

/**
 * @ClassName: CommonUtil
 * @Description:
 * @author zhuw
 * @date 2012-10-26 下午4:36:17
 */
public class AppTool {

	
	public static Toast  myShortToast= null;
	public static Toast  myLongToast= null;

    /**
     * 
     * @Description:TODO Toast short Msg
     * @param@param mContext
     * @param@param msg
     * @return void
     * @author zhuw
     * @date 2013-11-21 上午10:58:29
     */
	public static void tsMsg(Context mContext, String msg) {
		if(myShortToast == null){
			myShortToast = Toast.makeText(mContext, msg, Toast.LENGTH_SHORT);
		}else{
			myShortToast.setText(msg);
		}
		myShortToast.show();

	}
	/**
	 * 
	 * @Description:TODO Toast long Msg
	 * @param@param mContext
	 * @param@param msg
	 * @return void
	 * @author zhuw
	 * @date 2013-11-21 上午10:58:39
	 */
	public static void tlMsg(Context mContext, String msg) {
		if(myLongToast == null){
			myLongToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
		}else{
			myLongToast.setText(msg);
		}
		myLongToast.show();

	}
	/***
	 * 
	 * @Description: 启动Activity
	 * @param ctx
	 * @param packageName
	 */
	public static void startApkActivity(final Context ctx, String packageName) {
		PackageManager pm = ctx.getPackageManager();
		PackageInfo pi;
		try {
			pi = pm.getPackageInfo(packageName, 0);
			Intent intent = new Intent(Intent.ACTION_MAIN, null);
			intent.addCategory(Intent.CATEGORY_LAUNCHER);
			intent.setPackage(pi.packageName);

			List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);

			ResolveInfo ri = apps.iterator().next();
			if (ri != null) {
				String className = ri.activityInfo.name;
				intent.setComponent(new ComponentName(packageName, className));
				ctx.startActivity(intent);
			}
		} catch (NameNotFoundException e) {
			// Log.e("startActivity", e);
		}
	}

	/**
	 * 
	 * @Description:TODO退出程序
	 * @param@param mContext
	 * @return void
	 * @author zhuw
	 * @date 2013-11-21 上午11:01:48
	 */
	public static void exitApk(Context mContext) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setIcon(R.drawable.ic_launcher).setTitle("提示");
		builder.setMessage("是否退出程序?");

		builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				ExitApplication.getInstance().exit();
			}
		});
		builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.show();

	}
}
